import 'package:flutter_menu/biz/order/detail/detail_controller.dart';
import 'package:flutter_menu/biz/order/detail/price/price_controller.dart';
import 'package:flutter_menu/biz/order/order_pg.dart';
import 'package:flutter_menu/biz/order/widget/count_number_button.dart';
import 'package:flutter_menu/gen/assets.gen.dart';
import 'package:flutter_menu/res/app_design.dart';
import 'package:get/get.dart';

final buttonWidth = 106.w;
final buttonHeight = 77.w;

@Deprecated("废弃， 电子菜单app不展示时价输入弹窗，改为Pos端输入")
class PriceInputDialog extends GetView<PriceController> {
  const PriceInputDialog({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PriceController>(
        init: PriceController(),
        builder: (controller) {
          return Dialog(
            elevation: 0,
            backgroundColor: AppColors.bgBottomSheet,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(10.r)),
            child: Stack(
              clipBehavior: Clip.none,
              children: [
                Container(
                  padding: EdgeInsets.symmetric(vertical: 24.w),
                  width: 586.w,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(DetailController.to.productName,
                          style: TextStyle(
                              fontSize: 23.sp,
                              color: AppColors.textInBottomSheet,
                              fontWeight: AppStyle.semiBold)),
                      SizedBox(height: 32.w),
                      buildPriceText(),
                      SizedBox(height: 21.w),
                      Row(
                        children: [
                          Expanded(
                            child: GridView.count(
                              physics: const NeverScrollableScrollPhysics(),
                              crossAxisCount: 3,
                              crossAxisSpacing: 18.w,
                              mainAxisSpacing: 18.w,
                              shrinkWrap: true,
                              childAspectRatio: buttonWidth / buttonHeight,
                              children: PriceNumberKey.values.where((type) {
                                return controller.isInd
                                    ? (type != PriceNumberKey.dot)
                                    : (type != PriceNumberKey.doubleZero);
                              }).map((item) {
                                return _buildNumberButton(item.label,
                                    onTap: () => _onNumberTap(item));
                              }).toList(),
                            ),
                          ),
                          SizedBox(
                            width: 18.w,
                          ),
                          [
                            _buildNumberButton("priceInputClean".tr,
                                    onTap: controller.onClearTap)
                                .expanded(flex: 1),
                            SizedBox(
                              height: 18.w,
                            ),
                            _buildNumberButton(
                              "",
                              onTap: controller.onDeleteTap,
                              child: MyAssets.images.icDelete
                                  .image()
                                  .size(width: 35.w, height: 26.w),
                            ).expanded(flex: 1),
                          ].toColumn(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              mainAxisSize: MainAxisSize.max)
                        ],
                      ).paddingSymmetric(horizontal: 50.w).size(height: 372.w),
                      Padding(
                        padding: EdgeInsets.symmetric(vertical: 21.w),
                        child: const Divider(
                          height: 1,
                          color: AppColors.text_E2E2E2,
                        ),
                      ),
                      _buildActionButton(context),
                    ],
                  ),
                ),
                _buildCloseButton(),
              ],
            ),
          );
        });
  }

  _onNumberTap(PriceNumberKey t) {
    controller.onNumberTap(t);
  }

  void closePriceDialog() {
    if (Get.isDialogOpen == true) {
      Get.back();
    }
  }

  void resetPrice() {
    controller.resetPrice();
  }

  void savePrice() {
    if (Get.isDialogOpen == true) {
      Get.back();
    }
    controller.setNewPrice();
  }

  Widget buildPriceText() {
    var width = 62.w;
    return Container(
      height: width,
      decoration: BoxDecoration(
        border: Border.all(
          color: AppColors.priceInputBorderColor,
          width: 1.h,
        ),
      ),
      child: Stack(
        fit: StackFit.expand,
        children: [
          Positioned.fill(
            child: Container(
                color: Colors.white,
                alignment: Alignment.center,
                child: Obx(() {
                  return Text(controller.inputText.value,
                          style: TextStyle(
                              fontSize: 26.sp,
                              color: AppColors.textInBottomSheet,
                              fontWeight: AppStyle.semiBold))
                      .paddingSymmetric(horizontal: width);
                })),
          ),
          Positioned(
            left: 0,
            top: 0,
            bottom: 0,
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 20.w),
              alignment: Alignment.center,
              color: AppColors.countNumberButtonActive,
              child: Text(
                  controller.isInd
                      ? "currency_ind_unit".tr
                      : "currency_mex_unit".tr,
                  style: TextStyle(
                      fontSize: 23.sp, color: AppColors.textInBottomSheet)),
            ),
          ),
        ],
      ),
    ).paddingSymmetric(horizontal: 50.w);
  }

  Widget _buildNumberButton(String label,
      {VoidCallback? onTap, Widget? child}) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(6.r)),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.35), // Shadow color with opacity
            spreadRadius: 0, // Spread value
            blurRadius: 8, // Blur value
            offset: const Offset(0, 0), // Offset (horizontal, vertical)
          ),
        ],
      ),
      child: ElevatedButton(
        onPressed: onTap,
        style: ElevatedButton.styleFrom(
          backgroundColor: AppColors.countNumberBg,
          foregroundColor: AppColors.itemHighLight,
        ),
        child: child ??
            Text(
              label,
              style: TextStyle(
                  color: AppColors.textInBottomSheet, fontSize: 29.sp),
            ),
      ).size(width: buttonWidth),
    );
  }

  Positioned _buildCloseButton() {
    return Positioned(
      right: -35,
      top: -35,
      child: GestureDetector(
        onTap: () {
          closePriceDialog();
        },
        child: MyAssets.images.icClose
            .image()
            .size(width: 50.w, height: 50.w)
            .paddingAll(10.w),
      ),
    );
  }

  Widget _buildActionButton(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        OutlinedButton(
          onPressed: () {
            resetPrice();
          },
          style: OutlinedButton.styleFrom(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(9.r)),
            ),
            side: BorderSide(
              width: 1.5.w,
              color: AppColors.bgTab,
            ),
            backgroundColor: Colors.white,
            foregroundColor: AppColors.bgTab,
          ),
          child: Text('priceInputReset'.tr,
              style: TextStyle(
                  fontWeight: AppStyle.semiBold,
                  color: AppColors.textInBottomSheet,
                  fontSize: 22.sp)),
        ).size(height: 70.w).expanded(),
        ElevatedButton(
          onPressed: () {
            savePrice();
          },
          style: ElevatedButton.styleFrom(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.all(Radius.circular(9.r)),
            ),
            backgroundColor: AppColors.bgTab,
            foregroundColor: AppColors.bgTab.withOpacity(0.5),
          ),
          child: Text('priceInputSave'.tr,
              style: TextStyle(
                  fontWeight: AppStyle.semiBold,
                  color: Colors.white,
                  fontSize: 22.sp)),
        ).size(height: 70.w).marginOnly(left: 23.w).expanded(),
      ],
    ).paddingSymmetric(horizontal: 50.w);
  }
}
